//
//  SXBaseRefreshViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXBaseRefreshViewController.h"
#import "SXLoadingView.h"

@interface SXBaseRefreshViewController () <UITableViewDelegate>

@property (nonatomic, strong) SXLoadingView *loadingView; // 下拉刷新的页面
@property (nonatomic, assign) BOOL isLoading; // 正在刷新
@property (nonatomic, strong) UIImageView *loadingImageView;

@end

@implementation SXBaseRefreshViewController

- (void)dealloc
{
    _mainTableView.delegate = nil;
}

- (UITableView *)mainTableView
{
    _mainTableView = nil;
    _mainTableView.delegate = self;
    return _mainTableView;
}

- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (SXLoadingView *)loadingView
{
    if (!_loadingView) {
        _loadingView = [[SXLoadingView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    }
    return _loadingView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.mainTableView];
    
    _isLoading = NO;
    _isRefreshMore = YES;
    _loadingTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kScreenWidth, 50)];
    _loadingTitleLabel.textAlignment = NSTextAlignmentCenter;
    _loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 110, 10, 30, 30)];
    [_loadingTitleLabel addSubview:_loadingImageView];
    [_mainTableView addSubview:_loadingTitleLabel];
}

// 网络请求
- (void)requestData
{
    
}

// 下拉刷新

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollViewY = scrollView.contentOffset.y;
    // 判断是否被拖拽
    if (scrollView.isDragging) {
        if (scrollViewY > -50 && scrollViewY < 0) {
            _loadingTitleLabel.text = @"用力一点就可以刷新了";
            _loadingImageView.image = [UIImage imageNamed:@"SX-xiajiantou"];
        }
        if (scrollViewY < - 50) {
            _loadingTitleLabel.text = @"松开手就可以刷新了";
            _loadingImageView.image = [UIImage imageNamed:@"SX-shangjiantou"];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y < - 50)
    {
        self.mainTableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
        [_loadingTitleLabel addSubview:self.loadingView];
    }
    else if (scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.frame.size.height + 50 && _isRefreshMore)
    {
        self.mainTableView.contentInset = UIEdgeInsetsMake(-scrollView.contentSize.height + scrollView.frame.size.height - 50, 0, 0, 0);
        [self.mainTableView addSubview:self.loadingView];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= - 30 && _isLoading == NO)
    {
        _isLoading = YES;
        self.start = 0;
        [self requestData];
    }
    else if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height + 30 && _isLoading == NO && _isRefreshMore)
    {
        _isLoading = YES;
        self.loadingView.frame = CGRectMake(0, scrollView.contentSize.height, kScreenWidth, 50);
        self.start += self.limit;
        [self requestData];

    }
}

- (void)removeLoadingView
{
    _isLoading = NO;
    self.mainTableView.contentInset = UIEdgeInsetsZero;
    if (_loadingView) {
        [_loadingView removeFromSuperview];
        _loadingView = nil;
    }
}

// cell分割线从左边界开始
- (void)viewDidLayoutSubviews
{
    if ([self.mainTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.mainTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.mainTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.mainTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
